﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

using Microsoft.Office.Core;

using MongoDB.Bson;
using MongoDB.Driver;
using System.Diagnostics;
using ServiceStack.Redis;
////http://blog.csdn.net/yhrun/article/details/7674540
namespace word_xps_mongodb
{
    class Program
    {
        static List<string> chengyulist = new List<string>();
       static char[] chinachar = new char[] { '.', ' ', '‘', '、', '。', '，', '：', '；', '—', '*', '．', '─', '…', '□', '～', '→', '×' };
        static void Main(string[] args)
        {
            Getword();
            operateredis(chengyulist);
            //addchengyu_mongodb(chengyulist, "chengyu");
        }
      static  void operateredis(List<string> list)
        {
            string host = "localhost";
            string elementKey = "chengyu";
            using (RedisClient redisClient = new RedisClient(host))
            {
                if (redisClient.Get<string>(elementKey) == null)
                {                   
                    redisClient.Set<List<string>>(elementKey, list);
                }
                else
                {
                    //redisClient.Set<List<string>>(elementKey, list);
                    redisClient.Set<List<string>>(elementKey, list);

                    //redisClient.
                }               
                List<string> message = redisClient.Get<List<string>>(elementKey);
                Debug.WriteLine(message);
                foreach (var a in message)
                {
                    Debug.WriteLine(a);
                }
                Debug.WriteLine(message);             
                Console.ReadKey();
            }
        }
 
        private static void Getword()
        {
            try
            {
                Word.Application app = new Microsoft.Office.Interop.Word.Application();
                Word.Document doc = null;
                object unknow = Type.Missing;
                app.Visible = true;
                string str = @"d:\documents\visual studio 2017\Projects\word-xps-mongodb\word-xps-mongodb\chengyulist.doc";
                object file = str;
                doc = app.Documents.Open(ref file,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow, ref unknow,
                    ref unknow, ref unknow, ref unknow);
                for (int i = 1; i < doc.Paragraphs.Count; i++)
                {
                    string temp = doc.Paragraphs[i].Range.Text.Trim();
                    string[] array=  temp.Split(chinachar, StringSplitOptions.RemoveEmptyEntries);
                    foreach (var a in array)
                    {
                        if(a.Length==4)
                              {
                            chengyulist.Add(a);
                        }
                      
                    }                 
                    //Console.WriteLine(temp);
                    //Debug.WriteLine(temp);
                }
                foreach (var a in chengyulist)
                {
                    Debug.WriteLine(a);
                }
                Debug.WriteLine(chengyulist.Count+"count");

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }


        static async void addchengyu_mongodb(List<string> list,string collectionname)
        {
            var client = new MongoClient("mongodb://localhost:27017");       
            var database = client.GetDatabase("chinese_text");
            CreateCollectionOptions k = new CreateCollectionOptions();
    
           // database.CreateCollection(collectionname);
            var collection = database.GetCollection<BsonDocument>(collectionname);

            Dictionary<string, List<string>> dictionary = new Dictionary<string, List<string>>();
            dictionary.Add(collectionname, list);
            BsonDocument addbson = new BsonDocument(dictionary);
            await collection.InsertOneAsync(addbson);
          
            Console.ReadKey();
        }


    }
}
